package com.dongdongshop.service.impl;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.GoodsDescService;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.ItemService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ItemServiceImpl implements ItemService {
    //注入自己的配置
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Value("${freemarker.pageDir}")
    private String pageDir;

    //引用dubbo服务
    @Reference(check=false ,timeout=106666)
    private GoodsService goodsService;
    //引用dubbo服务
    @Reference(check=false ,timeout=106666)
    private GoodsDescService goodsDescService;
    @Reference(check=false ,timeout=106666)
    private ItemCatService itemCatService;

    @Override
    public void makeItem(Long id) {
        //加载配置
        Configuration configuration =freeMarkerConfigurer.getConfiguration();
        FileWriter fileWriter =null;
        try {

            Template template =configuration.getTemplate("item.ftl");
            //创建模型数据对象
            Map map = new HashMap();
            //查询goods表
            TbGoods tbGoods = goodsService.findGoodsById(id);
            //查询goodsdesc表
            TbGoodsDesc goodsDesc = goodsDescService.findGoodsDescById(id);
            map.put("goods",tbGoods);
            map.put("goodsDesc",goodsDesc);
            //查询分类
            Long category1Id = tbGoods.getCategory1Id();
            TbItemCat itemCat1 = itemCatService.getItemCatInfoById(category1Id);
            Long category2Id = tbGoods.getCategory2Id();
            TbItemCat itemCat2 = itemCatService.getItemCatInfoById(category2Id);
            Long category3Id = tbGoods.getCategory3Id();
            TbItemCat itemCat3 = itemCatService.getItemCatInfoById(category3Id);
            map.put("itemCat1",itemCat1);
            map.put("itemCat2",itemCat2);
            map.put("itemCat3",itemCat3);

            //查询item表
            List<TbItem> itemList = itemCatService.getItemInfoById(id);
            map.put("itemList",itemList);

            //创建输出流对象
            fileWriter =new FileWriter(pageDir+id+".html");
            //使用模板输出
            template.process(map,fileWriter);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关流
            if (fileWriter !=null){
                try {
                    fileWriter.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}
